<?php
declare (strict_types=1);

namespace app\admin\model\material;

use app\admin\logic\system\Config as SystemConfig;
use app\common\exception\LogicException;
use app\common\model\BaseModel;
use eduline\upload\stocks\bokecc\File;
use Exception;

/**
 * 定义素材分组模型
 */
class Category extends BaseModel
{
    /**
     * 表名
     *
     * @var string
     */
    protected $name = 'material_category';

    /**
     * 设置自动写入时间
     *
     * @var boolean
     */
    protected $autoWriteTimestamp = true;

    /**
     * 无需创建时间
     *
     * @var boolean
     */
    protected $createTime = false;
    /**
     * 时间显示格式
     *
     * @var string
     */
    protected $dateFormat = 'Y-m-d H:i';

    /**
     * 全局查询范围
     *
     * @var [type]
     */
    protected $globalScope = ['mhm'];

    /**
     * 定义表主键
     *
     * @var string
     */
    protected $pk = 'id';

    /**
     * 定义所有素材类型
     *
     * @var array
     */
    protected $materialTypes = ['picture' => 1, 'audio' => 2, 'video' => 3, 'file' => 4];

    /**
     * 设置素材类型
     * Author   Martinsun<syh@sunyonghong.com>
     * Date 2020-04-21
     *
     * @param    [type]                         $type [description]
     */
    public function setMaterialTypeAttr($type)
    {
        return $this->materialTypes[$type];
    }

    /**
     * 素材类型查询条件
     * Author   Martinsun<syh@sunyonghong.com>
     * Date 2020-04-21
     *
     * @param    [type]                         $query [description]
     * @param string $type [description]
     * @return void [type]                                [description]
     */
    public function scopeMaterialType($query, string $type = 'picture')
    {
        $type = $this->materialTypes[$type] ?? 0;
        $query->where('material_type', $type);
    }

    /**
     * 限制机构条件
     * Author   Martinsun<syh@sunyonghong.com>
     * Date 2020-06-16
     *
     * @param    [type]                         $query [description]
     * @return void [type]                                [description]
     */
    public function scopeMhm($query)
    {
        // 机构条件
        $query->where('mhm_id', request()->mhm_id);
    }

    /**
     * 新增数据事件
     * Author   Martinsun<syh@sunyonghong.com>
     * Date 2020-03-14
     *
     * @param $model
     */
    public static function onBeforeInsert($model)
    {
        // 机构
        $model->mhm_id = $model->getAttr('mhm_id') ?: request()->mhm_id;
    }

    public static function onAfterInsert(Category $model)
    {
        if ($model->material_type == 3) {
            $uploadConf = SystemConfig::get('system.package.upload', [], request()->mhm_id);
            $bokeccConf = SystemConfig::get('system.package.upload.bokecc', [], request()->mhm_id);
            $stock      = $uploadConf['stock'] ?? '';
            $userid     = $bokeccConf['userid'] ?? '';
            $apikey     = $bokeccConf['apikey'] ?? '';
            if ($stock == 'bokecc' && $userid && $apikey) {
                $bokecc = new File();
                try {
                    $bokecc->createCategory($model);
                } catch (Exception $e) {
                    throw new LogicException($e->getMessage());
                }
            }
        }
    }
}
